'''
Created on 2 dec 2011

@author: Adam
'''

class Cmd(object):
    '''
    The Sunray Engine interpreter that handels runtime scripting.
    '''
    scripts = {}
    
    def __init__(self):
        """Constructor"""
        
    def read(self, line):
        """
        Reads a line for the interpreter.
        
        Recommended use:
        while cmd.read(raw_input('>>> ')):
            pass
            
        if the input is '<<<' the cmd.read will return false.
        """
        
        #Check if the cmd should send a break command to a while loop.
        if line == "<<<":
            return False
        elif line == "cmds":
            for key in self.scripts.keys():
                self.write(key)
            return True
        
        try:
            data = line.split(' ', 1)
            
            script = self.scripts[data[0]]
            result = script(data[1])
            
            if result == None:
                return True
            else:
                self.write(result)
        except:
            self.write("Error")
            
        return True
        
    def write(self, data):
        print str(data)